package algotithm.leetcode.dp.hot.test152;

/**
 * @author zhouyanxiang
 * @Date 2021-02-2021/2/10-21:30
 * @URL https://leetcode-cn.com/problems/maximum-product-subarray/
 * @Title 152. 乘积最大子数组
 */
public class Solution {

    public static void main(String[] args) {
//        int[] nums = {2,3,-2,4};
        int[] nums = {-2,0,-1};
        Solution solution = new Solution();
        int max = solution.maxProduct(nums);
        System.out.println(max);
    }

    public int maxProduct(int[] nums) {
        int max = nums[0], min = nums[0], res = nums[0];
        for (int i = 1; i < nums.length; i++) {
            int mx = max, mn = min;
            max = Math.max(mx * nums[i], Math.max(nums[i],mn * nums[i]));
            min = Math.min(mn * nums[i], Math.min(nums[i],mx * nums[i]));
            res = Math.max(max,res);
        }
        return res;
    }

}
